package yxy.game.pm2.active.command.celebration;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.acttreasuresignup;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_国庆寻宝;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.NationalDayMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Collection;

/**
 * 国庆活动签到奖励领取接口
 *
 * @ClassName CMD_Active_国庆寻宝_NationalDayRegisterReward
 * @Author jingtao-zheng
 * @Date 2022/9/20 16:52
 * @Version 1.0
 **/
@MessageType(proto = Command.NationalDayRegisterReward, classtype = NationalDayMessage.NationalDayRegisterRewardRequest.class)
public class CMD_Active_国庆寻宝_NationalDayRegisterReward extends Command<NationalDayMessage.NationalDayRegisterRewardRequest> {
    @Override
    public void execute(MessageSource messageSource, NationalDayMessage.NationalDayRegisterRewardRequest request) throws Exception {
        String playercode = messageSource.getTag();
        int dataId = request.getDataId();
        NationalDayMessage.NationalDayRegisterRewardResponse.Builder response = NationalDayMessage.NationalDayRegisterRewardResponse.newBuilder();
        ActiveModule_国庆寻宝 am = ActiveModule_国庆寻宝.get(playercode, false);
        try {

            if (am == null) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            if (!am.running()) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }
            acttreasuresignup data = DataCenter.getData(dataId, acttreasuresignup.class);
            if (data == null) {
                send(messageSource, response, Result.DataError);
                return;
            }
            ArrayList<Integer> list = am.getHasRewardDays();
            if (list.contains(dataId)) {
                send(messageSource, response, Result.BottleWishRegisterCantRepeatedReward);//不能重复领取
                return;
            }
            CurrencyHandle currencyHandle = new CurrencyHandle();
            if (am.tryLock()) {
                list.add(dataId);
                Currencies curs = new Currencies(Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum()));
                Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
                currencyHandle.addCurrency(playercode, CCReason.国庆寻宝, curs);
                response.addAllDisplay(items);
                am.set();
            }

            response.setLogindays(am.getDay());//当前登录的天数
            response.addAllDrawDay(am.getHasRewardDays());//领取的天数
            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        } finally {
            am.unlock();
        }
    }

    private void send(MessageSource messageSource, NationalDayMessage.NationalDayRegisterRewardResponse.Builder response, Result
            result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
